﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace OCR
{
    /// <summary>
    /// 卡片类型管理类
    /// </summary>
    public class CardTypeManager
    {

        private static List<CardType> cardTypes;
        /// <summary>
        /// 卡片类型列表
        /// </summary>
        public static List<CardType> CardTypes
        {
            get
            {
                if (cardTypes == null)
                {
                    if (File.Exists(OCR.ConfigFilePath))
                    {
                        //从路径读取json数据
                        string configStr = File.ReadAllText(OCR.ConfigFilePath);
                        cardTypes = JsonConvert.DeserializeObject<List<CardType>>(configStr);
                        if(cardTypes==null)
                        {
                            cardTypes = new List<CardType>();
                        }
                        return cardTypes;
                    }
                    else
                    {
                        cardTypes = new List<CardType>();
                        return cardTypes;
                    }

                }
                else
                {
                    return cardTypes;
                }
            }
        }
        public static void AddCardType(CardType cardType)
        {
            cardTypes.Insert(0, cardType);
            Save();
        }
        public static void Del(CardType cardType)
        {
            cardTypes.Remove(cardType);
            Save();
        }

        public static void Save()
        {
            string orcJson = JsonConvert.SerializeObject(cardTypes);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(OCR.ConfigFilePath, false))
            {
                file.WriteLine(orcJson);
            }
        }
    }
}
